/*
	Copyright (C) 2008 by Jean-François Geyelin
	jfgeyelin@gmail.com

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the
	Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "all.h"
#include "structures.h"
#include "Menu.h"
#include "constants.h"
#include "Save.h"
#include "globals.h"
#include "playMap.h"
#include "readMap.h"


void transition(void);
int mapMenu(int, int);
bool saveFileExists(int);
void deleteSaveFile(int);

void addLevelsToMenu(Menu *menu) {
	char s[100];
	for (int i=0;i<NUMBER_OF_SLOTS-2;i++) {
		if (saveFileExists(i)) {
			Save save;
			save.readSave(i);
			sprintf(s,"Level %i",save.getNumberOfLevels());
		}
		else
		{
            sprintf(s,"Empty slot %i",i);
		}
		menu->addMenuItem(s, i);
	}
}



int chooseSaveSlot(void) {
	Menu menu;
	menu.addTitle("Choose saving slot");
	addLevelsToMenu(&menu);
	menu.setIcons(4,"empty.png","empty.png","empty.png","icon_back.png");
MENU_LABEL:;
	int result = menu.launch();
	if (result<0)
	return -1;
	if (saveFileExists(result)) {
		Menu menu2;
		menu2.addTitle("Overwrite this file?");
		menu2.addMenuItem("Yes",1);
		menu2.addMenuItem("No",0);
		if (menu2.launch()!=1)
		goto MENU_LABEL;
	}
	Save save;
	save.writeSave(result);
	return result;
}


int chooseLoadSlot(void) {
	Menu menu;
	menu.addTitle("Load slot");
	addLevelsToMenu(&menu);
	menu.setIcons(4,"empty.png","empty.png","empty.png","icon_back.png");
	int result;
	do {
		result = menu.launch();
		if (result<0)
		return -1;
		if (!saveFileExists(result)) {
			Menu menu2;
			menu2.addTitle("This slot is empty");
			menu2.addMenuItem("Ok");
			menu2.launch();
		}
	} while (!saveFileExists(result));
	quit = false;
	return result;
}


void chooseSlotToDelete(void) {
	Menu *menu= new Menu();
	menu->addTitle("Choose slot to delete");
	addLevelsToMenu(menu);
	menu->setIcons(4,"empty.png","empty.png","empty.png","icon_back.png");
	int result;
	do {
		result = menu->launch();
		if (result<0) {
			delete menu;
			return;
		}
		if (!saveFileExists(result)) {
			Menu menu2;
			menu2.addTitle("This slot is empty");
			menu2.addMenuItem("Ok");
			menu2.launch();
		}
		else
		{
			Menu menu2;
			menu2.addTitle("Are you sure?");
			menu2.addMenuItem("Yes",2);
			menu2.addMenuItem("No",1);
			if (menu2.launch()==2) {
				deleteSaveFile(result);
			    delete menu;
			    menu = new Menu();
			    menu->addTitle("Choose slot to delete");
				addLevelsToMenu(menu);
			}
		}
	} while (1);
}




void play(bool newGame) {
    PRINTF("Choosing save slot...\n");
	PlayerImportantInformations pii;
	int mapNumber;
	int slot=10;
	std::string maps[100];
	if (newGame) {
		slot = chooseSaveSlot();
	}
	else
	{

		slot = chooseLoadSlot();
	}

	if (slot<0)
	return;

    PRINTF("reading maps...\n");

	readMap(maps);

	Save s;
	s.readSave(slot);
	pii = *(s.getPii());

	quit = false;

    int levelChoice = s.getNumberOfLevels();



    while (!quit) {

		int n;

		n=s.getNumberOfLevels();

        mapNumber = mapMenu(n,levelChoice);


        if (mapNumber>=0) {
            PRINTF("waiting for transition...\n");
			transition();
			PRINTF("going to play a new game!\n");
			TEST_EXECUTION()
			if (playMap(maps[mapNumber], &pii, NULL, false)) {
				if (mapNumber==n) {
					s.increaseNumberOfLevels();
					levelChoice = n+1;
				}
			}
			musicThread->smoothlyStartPlayingMusic(MENU_MUSIC);
		}
		else
		{
            *(s.getPii()) = pii;
            s.writeSave(slot);
			return;
		}
	}
	*(s.getPii()) = pii;
	s.writeSave(slot);
	return;
}
